package cn.js.ccit.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.js.ccit.pojo.Meeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository;

@Repository
public class MeetingDAO implements IMeetingDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void addMeeting(Meeting meeting) {

		final String title=meeting.getTitle();
		final String place=meeting.getPlace();
		final String host=meeting.getHost();
		final String particiPant=meeting.getParticiPant();
		final String  departMent=meeting.getDepartMent();
		final String  meetingLevel=meeting.getMeetingLevel();
		final String budget=meeting.getBudget();
		final String startDate=meeting.getStartDate();
		final String startTime=meeting.getStartTime();
		final String endDate=meeting.getEndDate();
		final String endTime=meeting.getEndTime();
		final int stauts=meeting.getStauts();
		final String approvingPerson=meeting.getApprovingPerson();
		final String approvingDate=meeting.getApprovingDate();
		final String topic=meeting.getTopic();
		final String notes=meeting.getNotes();

		jdbcTemplate.update("insert into tb_meeting(title,place,host,particiPant,departMent,meetingLevel,budget,startDate,startTime,endDate,endTime,stauts,approvingPerson,approvingDate,topic,notes) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",new PreparedStatementSetter(){

			public void setValues(PreparedStatement ps)throws SQLException{
				ps.setString(1, title);
				ps.setString(2, place);
				ps.setString(3, host);
				ps.setString(4, particiPant);
				ps.setString(5, departMent);
				ps.setString(6, meetingLevel);
				ps.setString(7, budget);
				ps.setString(8, startDate);
				ps.setString(9, startTime);
				ps.setString(10, endDate);
				ps.setString(11, endTime);
				ps.setInt(12, stauts);
				ps.setString(13, approvingPerson);
				ps.setString(14, approvingDate);
				ps.setString(15, topic);
				ps.setString(16, notes);


			}
		});

	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	////获取所有会议信息
	public List getAllMeeting() {
		String sql="select * from tb_meeting";
		List meetingList=jdbcTemplate.queryForList(sql);
		return meetingList;
	}

	//根据Id获取会议信息
	public Meeting getMeetingById(int id){

		String sql="select * from tb_meeting where id="+id;

		List meetingList=jdbcTemplate.queryForList(sql);

		Iterator it =meetingList.iterator();

		Meeting meeting=new Meeting();
		while(it.hasNext()) {
			Map map = (Map) it.next();
			meeting.setId(Integer.parseInt(""+map.get("id")));
			System.out.println("id="+meeting.getId());

			meeting.setTitle(""+map.get("title"));
			System.out.println("title="+meeting.getTitle());

			meeting.setPlace(""+map.get("place"));
			meeting.setHost(""+map.get("host"));

			meeting.setParticiPant(""+map.get("particiPant"));
			meeting.setDepartMent(""+map.get("departMent"));

			meeting.setMeetingLevel(""+map.get("meetingLevel"));
			meeting.setBudget(""+map.get("budget"));
			meeting.setStauts(Integer.parseInt(""+map.get("stauts")));
			meeting.setStartDate(""+map.get("startDate"));
			meeting.setStartTime(""+map.get("startTime"));
			meeting.setEndDate(""+map.get("endDate"));
			meeting.setEndTime(""+map.get("endTime"));
			meeting.setTopic((""+map.get("topic")).trim());
			meeting.setNotes(""+map.get("notes"));
		}


		return meeting;
	}

	//审批会议
	public boolean approvalMeeting(Meeting meeting) {
		boolean flag=false;
		int id=meeting.getId();
		int stauts=0;
		String approvingPerson="";
		String approvingDate="";

		int i = jdbcTemplate
				.update(
						"update tb_meeting set stauts=?,approvingPerson=?,approvingDate=? where id=?",
						new Object[] { meeting.getStauts(),meeting.getApprovingPerson(),meeting.getApprovingDate(),meeting.getId() });
		if (i > 0) {
			flag = true;
		}
		return flag;
	}


	//会议查询
	public List queryMeeting(String title,String departMent){
		String sql="";
		List meetingList=null;


		title=title.trim();
		departMent=departMent.trim();
		System.out.println("title="+title.trim().equals(""));
		System.out.println("departMent="+departMent.trim().equals(""));

		if(title.equals("") && departMent.equals("")){
			sql="select * from tb_meeting ";
			meetingList=jdbcTemplate.queryForList(sql);
		}else if(!title.equals("") && departMent.equals("")){
			meetingList=jdbcTemplate.queryForList("select * from tb_meeting where title=?",new Object[] {title});
		}else if(!title.equals("") && departMent.equals("")){
			meetingList=jdbcTemplate.queryForList("select * from tb_meeting where departMent=?",new Object[] {departMent});
		}else{
			meetingList=jdbcTemplate.queryForList("select * from tb_meeting where title=? and departMent=?",new Object[] {title,departMent});
		}

		System.out.println("meetingList.size="+meetingList.size());
		return meetingList;
	}
}
